package com.example.mapper;

import cn.hutool.core.collection.CollUtil;
import com.example.domain.AdminUserDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class AdminUserMapper {
    /**
     * 存放默认用户信息
     */
    private final List<AdminUserDetails> adminUserDetailsList = new ArrayList<>();

    private final BCryptPasswordEncoder passwordEncoder;

    @PostConstruct
    private void init(){
        adminUserDetailsList.add(AdminUserDetails.builder()
                .username("admin")
                .password(passwordEncoder.encode("123456"))
                .authorityList(CollUtil.toList(
                        "1:user:create","2:user:update","3:user:delete",
                        "4:user:get","5:user:list"))
                .build());

        adminUserDetailsList.add(AdminUserDetails.builder()
                .username("guest")
                .password(passwordEncoder.encode("123456"))
                .authorityList(CollUtil.toList("4:user:get","5:user:list"))
                .build());
    }

    public AdminUserDetails getByUsername(String username){
       return adminUserDetailsList.stream()
                .filter(item -> item.getUsername().equals(username))
                .findFirst()
                .orElse(null);
    }

    public List<AdminUserDetails> list(){
        return adminUserDetailsList;
    }
}
